package view;

import java.awt.AlphaComposite;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Composite;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Image;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Observable;
import java.util.Observer;
import java.util.Random;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import org.omg.CosNaming.IstringHelper;

import mode.Chess;
import mode.ChessArray;
import mode.User;
import util.SocketUtil;
import util.StateCode;

public class ChessPanel extends JPanel implements ActionListener, Observer {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private GridBagPanel gbp;
	private String[] s = { "开始", "争庄", "悔棋", "和棋", "认输", "回放" };
	private JButton[] but = new JButton[s.length];
	private JLabel[] player = new JLabel[2];
	private JLabel image;
	private ChessArray chessArray;
	private Chess chess;
	private int isBest;
	private int randomValue;
	private boolean compare = false;

	public ChessPanel() {
		// TODO Auto-generated constructor stub
		setBackground(Color.yellow);
		setLayout(new BorderLayout());
		setOpaque(false);

		chessArray = new ChessArray();
		gbp = new GridBagPanel(chessArray);
		add(gbp);
		chessArray.addObserver(gbp);

		JPanel bottom = new JPanel();
		bottom.setOpaque(false);

		for (int i = 0; i < s.length; i++) {
			but[i] = new JButton(s[i]);
			bottom.add(but[i]);
			but[i].addActionListener(this);
		}
		buttonInitState();
		add(bottom, BorderLayout.SOUTH);

		JPanel top = new JPanel();
		top.setOpaque(false);
		JLabel title = new JLabel("网络五子棋");
		title.setFont(new Font("华文琥珀", Font.TRUETYPE_FONT, 31));
		title.setForeground(new Color(0x912CEE));
		top.add(title);
		JLabel developer = new JLabel("        Co.Ltd,华信智源");
		developer.setFont(new Font("宋体", Font.TRUETYPE_FONT, 17));
		developer.setForeground(new Color(0xFFFF00));
		top.add(developer);
		add(top, BorderLayout.NORTH);

		String[] s1 = { "玩家1", "玩家2" };
		JPanel[] jp = new JPanel[2];
		for (int i = 0; i < s1.length; i++) {
			jp[i] = new JPanel();
			jp[i].setOpaque(false);
			jp[i].setLayout(new GridBagLayout());
			GridBagConstraints gbc = new GridBagConstraints();
			image = new JLabel();
			image.setIcon(new ImageIcon(getClass().getResource("../image/" + (i + 1) + ".jpg")));
			player[i] = new JLabel(s1[i]);
			gbc.gridy = 0;
			gbc.insets = new Insets(10, 12, 10, 10);
			jp[i].add(image, gbc);
			gbc.gridy = 1;
			jp[i].add(player[i], gbc);
		}

		add(jp[0], BorderLayout.WEST);
		add(jp[1], BorderLayout.EAST);
	}

	@Override
	protected void paintComponent(Graphics g) {
		// TODO Auto-generated method stub
		super.paintComponent(g);
		Graphics2D g2 = (Graphics2D) g;
		Composite composite = g2.getComposite();
		g2.setComposite(AlphaComposite.SrcOver.derive(0.7f));
		Image image = new ImageIcon(getClass().getResource("../image/background.jpg")).getImage();
		g.drawImage(image, 0, 0, getWidth(), getHeight(), null);
		g2.setComposite(composite);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		if (e.getSource() == but[1]) {
			Random rd = new Random();
			randomValue = rd.nextInt(100) * 100000;
			if (!compare)
				gbp.setIsMouseListener(StateCode.WAITSELECTBANKER);
			if (!compare)
				SocketUtil.sendMessage(randomValue);
			if (compare) {
				if (isBest >= randomValue) {
					gbp.setIsMouseListener(StateCode.WAITBANKERCHESS);
					SocketUtil.sendMessage(StateCode.SELECTCOLOR);
					JOptionPane.showConfirmDialog(this, "对方为庄，对方先落子！");
					SocketUtil.sendMessage(StateCode.ISFIRST);
					buttonStartedState();
				} else {
					gbp.setIsMouseListener(StateCode.SELECTCOLOR);
					SocketUtil.sendMessage(StateCode.WAITBANKERCHESS);
					JOptionPane.showMessageDialog(this, "我方为庄，我方先落子！");
					SocketUtil.sendMessage(StateCode.ISOPPOSITEFIRST);
					buttonStartedState();
				}
			}
			if (!compare)
				SocketUtil.sendMessage(StateCode.SELECTBAKER);
			buttonStartedState();
		} else if (e.getSource() == but[0]) {
			SocketUtil.sendMessage(StateCode.REQUESTSTART);
			but[0].setEnabled(false);
			but[1].setEnabled(true);
		} else if (e.getSource() == but[2]) {
			SocketUtil.sendMessage(StateCode.REGRET);
		} else if (e.getSource() == but[3]) {
			SocketUtil.sendMessage(StateCode.EQUAL);
		} else if (e.getSource() == but[4]) {
			SocketUtil.sendMessage(StateCode.ISLOSER);
		} else if (e.getSource() == but[5]) {
			gbp.setSavePanit(true);
			gbp.setChessarray(chessArray.replay());
			gbp.repaint();
		}
	}

	@Override
	public void update(Observable o, Object arg) {
		// TODO Auto-generated method stub
		if (arg instanceof User) {
			User user = new User();
			if (o.equals(user))
				player[0].setText(user.getName());
		} else if (arg instanceof Chess) {
			chess = (Chess) arg;
			System.out.println(arg);
			chessArray.saveOppositeChess(chess);
			gbp.setChess(true);
		} else if (arg instanceof Integer) {
			if ((int) arg > 10000) {
				isBest = (int) arg;
				compare = true;
			} else if ((int) arg == StateCode.SELECTCOLOR) {
				gbp.setIsMouseListener(StateCode.SELECTCOLOR);
				but[1].setEnabled(false);
			} else if ((int) arg == StateCode.WAITBANKERCHESS) {
				gbp.setIsMouseListener(StateCode.WAITBANKERCHESS);
				but[1].setEnabled(false);
			} else if ((int) arg == StateCode.WAITOPPOSITECHESS) {
				gbp.setIsMouseListener(StateCode.WAITOPPOSITECHESS);
			} else if ((int) arg == StateCode.ISMOUSELISTENER) {
				gbp.setIsMouseListener(StateCode.ISMOUSELISTENER);
			} else if ((int) arg == StateCode.Black) {
				gbp.setSelectValue(StateCode.WHITE);
			} else if ((int) arg == StateCode.WHITE) {
				gbp.setSelectValue(StateCode.Black);
			} else if ((int) arg == StateCode.REQUESTSTART) {
				int f = JOptionPane.showConfirmDialog(this, "有人请求开始,是否同意开始？", "提示信息", JOptionPane.YES_NO_OPTION);
				if (f == 0) {
					SocketUtil.sendMessage(StateCode.AGREESTART);
					but[0].setEnabled(false);
					but[1].setEnabled(true);
				} else {
					SocketUtil.sendMessage(StateCode.DISAGREESTART);
				}
			} else if ((int) arg == StateCode.AGREESTART) {
				int f = JOptionPane.showConfirmDialog(this, "对方同意开始，是否同意？", "提示信息", JOptionPane.YES_NO_OPTION);
				if (f == 0) {
					JOptionPane.showMessageDialog(this, "请点击“争庄”按钮，选择谁先落子。");
				} else {
					SocketUtil.sendMessage(StateCode.DISAGREESTART);
				}
			} else if ((int) arg == StateCode.DISAGREESTART) {
				JOptionPane.showInternalMessageDialog(this, "对方拒绝开始！");
			} else if ((int) arg == StateCode.SELECTBAKER) {
				JOptionPane.showMessageDialog(this, "请点击“争庄”按钮，选择谁先落子。");
			} else if ((int) arg == StateCode.ISFIRST) {
				JOptionPane.showMessageDialog(this, "我方为庄，我方先落子！");
			} else if ((int) arg == StateCode.ISOPPOSITEFIRST) {
				JOptionPane.showMessageDialog(this, "对方为庄，对方先落子！");
			} else if ((int) arg == StateCode.ISWIN) {
				if (gbp.getSelectValue() == StateCode.Black) {
					JOptionPane.showMessageDialog(this, "黑棋已经赢了！");
					SocketUtil.sendMessage(StateCode.ISNOTIFYWIN);
					buttonOverState();
				} else if (gbp.getSelectValue() == StateCode.WHITE) {
					JOptionPane.showMessageDialog(this, "白棋已经赢了！");
					SocketUtil.sendMessage(StateCode.ISNOTIFYWIN);
					buttonOverState();
				}
			} else if ((int) arg == StateCode.ISNOTIFYWIN) {
				if (gbp.getSelectValue() == StateCode.Black) {
					JOptionPane.showMessageDialog(this, "白棋已经赢了！");
					gbp.setIsMouseListener(StateCode.INITLISTENER);
					SocketUtil.sendMessage(StateCode.INITLISTENER);
					buttonOverState();
				} else if (gbp.getSelectValue() == StateCode.WHITE) {
					JOptionPane.showMessageDialog(this, "黑棋已经赢了！");
					gbp.setIsMouseListener(StateCode.INITLISTENER);
					SocketUtil.sendMessage(StateCode.INITLISTENER);
					buttonOverState();
				}
			} else if ((int) arg == StateCode.INITLISTENER) {
				gbp.setIsMouseListener(StateCode.INITLISTENER);
			} else if ((int) arg == StateCode.REGRET) {
				int f = JOptionPane.showConfirmDialog(this, "对方请求悔棋，是否同意？", "提示信息", JOptionPane.YES_NO_OPTION);
				if (gbp.getIsMouseListener() == StateCode.ISMOUSELISTENER) {
					if (f == 0) {
						chessArray.regret();
						SocketUtil.sendMessage(StateCode.AGREEREGRET);
					} else {
						SocketUtil.sendMessage(StateCode.DISAGREEREGRET);
					}
				} else if (gbp.getIsMouseListener() == StateCode.WAITOPPOSITECHESS) {
					if (f == 0) {
						chessArray.regret();
						SocketUtil.sendMessage(StateCode.AGREEREGRET);
					} else {
						SocketUtil.sendMessage(StateCode.DISAGREEREGRET);
					}
				}
			} else if ((int) arg == StateCode.AGREEREGRET) {
				if (gbp.getIsMouseListener() == StateCode.ISMOUSELISTENER) {
					chessArray.regret();
					gbp.setIsMouseListener(StateCode.WAITOPPOSITECHESS);
					SocketUtil.sendMessage(StateCode.ISMOUSELISTENER);
				} else if (gbp.getIsMouseListener() == StateCode.WAITOPPOSITECHESS) {
					chessArray.regret();
					gbp.setIsMouseListener(StateCode.ISMOUSELISTENER);
					SocketUtil.sendMessage(StateCode.WAITOPPOSITECHESS);
				}
			} else if ((int) arg == StateCode.DISAGREEREGRET) {
				JOptionPane.showMessageDialog(this, "对方不同意悔棋！");
			} else if ((int) arg == StateCode.EQUAL) {
				int f = JOptionPane.showConfirmDialog(this, "对方请求和棋，是否同意？", "提示信息", JOptionPane.YES_NO_OPTION);
				if (f == 0) {
					SocketUtil.sendMessage(StateCode.AGREEEQUAL);
					JOptionPane.showMessageDialog(this, "该局为平局！");
				} else {
					JOptionPane.showMessageDialog(this, StateCode.DISAGREEEQUAL);
				}
			} else if ((int) arg == StateCode.AGREEEQUAL) {
				JOptionPane.showMessageDialog(this, "该局为平局！");
				gbp.setIsMouseListener(StateCode.INITLISTENER);
				SocketUtil.sendMessage(StateCode.INITLISTENER);
				buttonOverState();
			} else if ((int) arg == StateCode.DISAGREEEQUAL) {
				JOptionPane.showMessageDialog(this, "对方拒绝了和棋！");
			} else if ((int) arg == StateCode.ISLOSER) {
				int f = JOptionPane.showConfirmDialog(this, "对方请求认输，是否同意", "提示信息", JOptionPane.YES_NO_OPTION);
				if (f == 0) {
					if (gbp.getSelectValue() == StateCode.Black) {
						JOptionPane.showMessageDialog(this, "黑棋赢了！");
						gbp.setIsMouseListener(StateCode.INITLISTENER);
						SocketUtil.sendMessage(StateCode.INITLISTENER);
						SocketUtil.sendMessage(StateCode.AGREEISLOSER);
						buttonOverState();
					} else if (gbp.getSelectValue() == StateCode.WHITE) {
						JOptionPane.showMessageDialog(this, "白棋赢了！");
						gbp.setIsMouseListener(StateCode.INITLISTENER);
						SocketUtil.sendMessage(StateCode.INITLISTENER);
						SocketUtil.sendMessage(StateCode.AGREEISLOSER);
						buttonOverState();
					}
				} else {
					SocketUtil.sendMessage(StateCode.DISAGREEISLOSER);
				}
			} else if ((int) arg == StateCode.AGREEISLOSER) {
				if (gbp.getSelectValue() == StateCode.Black) {
					JOptionPane.showMessageDialog(this, "白棋赢了！");
					gbp.setIsMouseListener(StateCode.INITLISTENER);
					SocketUtil.sendMessage(StateCode.INITLISTENER);
					buttonOverState();
				} else if (gbp.getSelectValue() == StateCode.WHITE) {
					JOptionPane.showMessageDialog(this, "黑棋赢了！");
					gbp.setIsMouseListener(StateCode.INITLISTENER);
					SocketUtil.sendMessage(StateCode.INITLISTENER);
					buttonOverState();
				}
			} else if ((int) arg == StateCode.DISAGREEISLOSER) {
				JOptionPane.showMessageDialog(this, "对方不同意认输请求！");
			}
		}
	}

	private void buttonInitState() {
		// TODO Auto-generated method stub
		but[1].setEnabled(false);
		but[2].setEnabled(false);
		but[3].setEnabled(false);
		but[4].setEnabled(false);
		but[5].setEnabled(false);
	}

	private void buttonStartedState() {
		but[0].setEnabled(false);
		but[1].setEnabled(false);
		but[2].setEnabled(true);
		but[3].setEnabled(true);
		but[4].setEnabled(true);
		but[5].setEnabled(false);
	}

	private void buttonOverState() {
		but[0].setEnabled(false);
		but[1].setEnabled(false);
		but[2].setEnabled(false);
		but[3].setEnabled(false);
		but[4].setEnabled(false);
		but[5].setEnabled(true);
	}
}
